package com.wheel.autoconfigure.web.mvc.filter;/*
 * <h3>Title:fis-api系统</h3>
 * <p>Copyright (c) 2020, wangnaifei@163.com. All rights reserved.</p>
 * @author wangnaifei@163.com
 */

import com.wheel.core.time.WheelDateTime;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <dt>Description:ResponseTimeAOP</dt>
 * <dd>方法响应时间处理</dd>
 *
 * @author wangnaifei@163.com
 * @version 1.0 <i>2020/10/30 10:14</i>
 * @since jdk1.7+
 */
public class ResponseTimeFilter extends CacheExceptionOncePerRequestFilter {

    public static final String REQUEST_TIMESTAMP_KEY = "WheelRequestTimestamp";

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws Exception {
        request.setAttribute(REQUEST_TIMESTAMP_KEY, WheelDateTime.nowTimestamp());
        chain.doFilter(request, response);
    }
}

//@Aspect
//public class ResponseTimeFilter {
//
//    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
//    void hasRequestMappingAnnotation() {}
//
//    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
//    void hasGetMappingAnnotation() {}
//
//    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
//    void hasPostMappingAnnotation() {}
//
//    @Around("hasRequestMappingAnnotation() || hasGetMappingAnnotation() || hasPostMappingAnnotation()")
//    public Object handleTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        long startTime = System.currentTimeMillis();
//        Object proceed = proceedingJoinPoint.proceed();
//        if (proceed instanceof Result) {
//            Result<?> obj = (Result<?>) proceed;
//            long endTime = System.currentTimeMillis();
//            obj.setResponseTime(endTime - startTime);
//        }
//        return proceed;
//    }
//}